from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from .models import HardContract, SoftContract
from datetime import date

class SubmitHardContractView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        data = request.data
        contract = HardContract.objects.create(
            user=request.user,
            sign_date=date.today(),
            start_time=data['start_time'],
            end_time=data['end_time'],
            site=data['site'],
            content=data['content'],
            image_path=data.get('image_path'),
            video_path=data.get('video_path'),
            status='待审核'
        )
        return Response({"message": "硬广合同已提交", "id": contract.id})

class SubmitSoftContractView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        data = request.data
        contract = SoftContract.objects.create(
            user=request.user,
            sign_date=date.today(),
            start_time=data['start_time'],
            end_time=data['end_time'],
            media=data['media'],
            need=data['need']
        )
        return Response({"message": "软广合同已提交", "id": contract.id})

from django.shortcuts import render, redirect
from .models import HardContract
from django.contrib.auth.decorators import login_required
from datetime import date

@login_required
def hard_contract_form(request):
    if request.method == 'POST':
        user = request.user
        data = request.POST
        contract = HardContract.objects.create(
            user=user,
            sign_date=date.today(),
            start_time=data['start_time'],
            end_time=data['end_time'],
            site=data['site'],
            content=data['content'],
            image_path=data.get('image_path'),
            video_path=data.get('video_path'),
            status='待审核'
        )
        return redirect('advertiser_dashboard')  # 提交后重定向到广告主仪表盘

    return render(request, 'hard_contract_form.html')
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from .models import HardContract

@login_required
def my_contracts_view(request):
    contracts = HardContract.objects.filter(user=request.user).order_by('-sign_date')
    return render(request, 'my_contracts.html', {'contracts': contracts})

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from .models import HardContract

# @login_required
# def advertiser_dashboard(request):
#     if request.user.role != 'advertiser':
#         return redirect('/')  # 非广告主重定向回主页或其他页
#
#     contracts = HardContract.objects.filter(user=request.user).order_by('-sign_date')
#     return render(request, 'advertiser_dashboard.html', {'contracts': contracts})
@login_required
def advertiser_dashboard(request):
    if request.user.role != 'advertiser':
        return redirect('/')

    # 这里从数据库查当前登录用户的合同
    hard_contracts = HardContract.objects.filter(user=request.user).order_by('-sign_date')
    soft_contracts = SoftContract.objects.filter(user=request.user).order_by('-sign_date')

    return render(request, 'advertiser_dashboard.html', {
        'hard_contracts': hard_contracts,
        'soft_contracts': soft_contracts,
    })

from .models import SoftContract
from django.utils import timezone

@login_required
def soft_contract_form(request):
    if request.user.role != 'advertiser':
        return redirect('/')

    if request.method == 'POST':
        data = request.POST
        SoftContract.objects.create(
            user=request.user,
            sign_date=date.today(),
            start_time=data['start_time'],
            end_time=data['end_time'],
            media=data['media'],
            need=data['need']
        )
        return redirect('advertiser_dashboard')
    return render(request, 'soft_contract_form.html')


